package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.Result;
import com.atguigu.yygh.common.exception.ziDyException;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //上传医院信息
    @ApiOperation("上传医院信息")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //这个是内部的api接口，需要校验
        String hoscode = request.getParameter("hoscode");
        if(StringUtils.isEmpty(hoscode)){
            throw new ziDyException(20001,"hoscode不存在");
        }
        //查询传过来的sgin码
        String sign = request.getParameter("sign");
        //根据hoscode来查询数据库存在的sgin码
        String signHosp = hospitalSetService.getSign(hoscode);
        String encrypt = MD5.encrypt(signHosp);

        if(!sign.equals(encrypt)){
            throw new ziDyException(20001,"sgin_key不正确");
        }

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)parameterMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        parameterMap.put("logoData",logoData);

        hospitalService.save(parameterMap);

        return Result.ok();
    }

    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {

        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String)map.get("hoscode");

        Hospital hospital = hospitalService.show(hoscode);

        return Result.ok(hospital);

    }

    @ApiOperation(value = "上传科室信息")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){

        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        departmentService.save(map);

        return Result.ok();

    }

    @ApiOperation(value = "查询科室全部信息，分页")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        Page<Department> pageModel = departmentService.selectPage(paramMap);

        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {

        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String)map.get("hoscode");
        String depcode = (String)map.get("depcode");

        departmentService.deleteById(hoscode,depcode);

        return Result.ok();
    }


    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        scheduleService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());

        Page<Schedule> all = scheduleService.findPage(switchMap);

        return Result.ok(all);
    }

    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String) map.get("hoscode");
        String hosScheduleId = (String) map.get("hosScheduleId");

        scheduleService.deleteById(hoscode,hosScheduleId);

        return Result.ok();

    }

}
